package com.allenkerr.util;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;

import java.util.Map;
import java.util.Optional;

/**
 * 搜索参数处理工具类
 * Created by 小柯 on 17.8.15 22:16.
 */
public class SearchParamUtils {

    public static String getStringParam(Map<String, String> map, String key) {
        return Optional.ofNullable(map)
                .map(stringMap -> stringMap.get(key))
                .map(String::trim)
                .filter(StringUtils::isNotEmpty)
                .orElse(null);
    }

    public static Long getLongParam(Map<String, String> map, String key) {
        return Optional.ofNullable(map)
                .map(stringMap -> stringMap.get(key))
                .map(NumberUtils::toLong)
                .orElse(null);
    }

    public static long getLongParam(Map<String, String> map, String key, long defaultValue) {
        return Optional.ofNullable(map)
                .map(stringMap -> stringMap.get(key))
                .map(str -> NumberUtils.toLong(str, defaultValue))
                .orElse(defaultValue);
    }

    public static Integer getIntegerParam(Map<String, String> map, String key) {
        return Optional.ofNullable(map)
                .map(stringMap -> stringMap.get(key))
                .map(NumberUtils::toInt)
                .orElse(null);
    }

    public static int getIntegerParam(Map<String, String> map, String key, int defaultValue) {
        return Optional.ofNullable(map)
                .map(stringMap -> stringMap.get(key))
                .map(str -> NumberUtils.toInt(str, defaultValue))
                .orElse(defaultValue);
    }

    public static Short getShortParam(Map<String, String> map, String key) {
        return Optional.ofNullable(map)
                .map(stringMap -> stringMap.get(key))
                .map(NumberUtils::toShort)
                .orElse(null);
    }

    public static short getShortParam(Map<String, String> map, String key, short defaultValue) {
        return Optional.ofNullable(map)
                .map(stringMap -> stringMap.get(key))
                .map(str -> NumberUtils.toShort(str, defaultValue))
                .orElse(defaultValue);
    }

    public static Byte getByteParam(Map<String, String> map, String key) {
        return Optional.ofNullable(map)
                .map(stringMap -> stringMap.get(key))
                .map(NumberUtils::toByte)
                .orElse(null);
    }

    public static byte getByteParam(Map<String, String> map, String key, byte defaultValue) {
        return Optional.ofNullable(map)
                .map(stringMap -> stringMap.get(key))
                .map(str -> NumberUtils.toByte(str, defaultValue))
                .orElse(defaultValue);
    }

    private SearchParamUtils() {
    }
}
